package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.POJO.Address;
import com.itheima.dao.MapMapper;
import com.itheima.dao.orderMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.service.MapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = MapService.class)
@Transactional
public class MapServiceImpl implements MapService {
    @Autowired
    private MapMapper mapMapper;

    @Autowired
    private com.itheima.dao.orderMapper orderMapper;

    @Override
    public PageResult findAddress(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Address>page=mapMapper.findAddress(queryPageBean);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteAddress(String id) {
        //查询中间表
        Integer count=orderMapper.findOrderByAddressId(id);
        if (count>0){
            throw new RuntimeException("已关联用户,无法删除");
        }
        mapMapper.deleteAddress(id);
    }

    @Override
    public List<Address> getAddress() {
        List<Address> list=mapMapper.getAddress();
        return list;
    }

    @Override
    public void addAddress(String longitude, String latitude, String address) {
        mapMapper.addAddress(longitude,latitude,address);
    }
}
